<script setup>
import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
import { onLoad, onShow } from '@dcloudio/uni-app'

import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
import { ref } from 'vue'
import dayjs from 'dayjs'
import optionDeptPerson from '@/store/modules/optionDeptPerson'
import {
  addFinancialApplyControllerApi,
  attendanceApplyApi,
  selectOrderNoApi,
} from '@/api/attendance'
import { getDeptTree,listTreeApi } from '@/api/dep'
import { getUserProfile } from '@/api/system/user'
import store from '@/store'
import approvalProcessState from '@/store/modules/approvalProcess'
import { isObjectValuesNotEmpty } from '@/utils/objIsnull.js'
const title = '文旅运营专项付款申请'
const costTypeRef = ref(null)
const paymentMethodsRef = ref(null)
const OperatingPaymentsList = ref({
  assumptionCostsDep:'',
  reasonPayment: '',
  costType: '',
  paymentMoney: '',
  paymentMethods: '',
  paymentDate: '',
  payee: '',
  bankAccount: '',
  bankName: '',
  actualDate: '',
  remark: '',
  deptId:''
})

const formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`
  }
  if (type === 'month') {
    return `${value}月`
  }
  if (type === 'day') {
    return `${value}日`
  }
  if (type === 'hour') {
    return `${value}时`
  }
  if (type === 'minute') {
    return `${value}分`
  }
  return value
}

const costTypeColumns = [
  [
    '外协材料',
    '外协工费',
    '办公费',
    '工资',
    '业务招待费',
    '汽车费用',
    '学习费',
    '差旅费',
    '税金',
    '投标保证金',
    '其它',
  ],
]

const assumptionCostsDepNumParams = ref(null)
const costTypeConfirm = (e) => {
  OperatingPaymentsList.value.costType = e.value[0]
}
const costTypeFn = () => {
  costTypeRef.value.open()
}

// //费用承担部门
// const assumptionCostsDepRef = ref(null)
// const assumptionCostsDepFn = () => {
// 	console.log(assumptionCostsDepRef.value);
//   assumptionCostsDepRef.value.open()
// }

// let selectedValue = []
// const assumptionCostsDepColumns = [
// 	[{value: 8801, label: '花境项目部'},{value: 8602, label: '传统旅游项目部'}],
// ]

// const assumptionCostsDepConfirm = (e) => {
// 	// console.log(assumptionCostsDepColumns[0]);
// 	console.log(e);
//   OperatingPaymentsList.value.assumptionCostsDep = e.value[0].label
//   // 审批绑定的费用承担部门  传参
//   assumptionCostsDepNumParams.value = e.indexs[0] + 1
// }

//费用承担部门2
const open = ref(false)
const changeSelect = (item, index) => {
  OperatingPaymentsList.value.deptId  = item.id
  OperatingPaymentsList.value.assumptionCostsDep  = item.label
}

const paymentMethodsColumns = [['转账', '微信', '支付宝', '现金']]
const paymentMethodsConfirm = (e) => {
  OperatingPaymentsList.value.paymentMethods = e.value[0]
}
const paymentMethodsFn = () => {
  paymentMethodsRef.value.open()
}


//付款日期选择
const paymentDateRef = ref(null)
const paymentDateFn = () => {
	paymentDatePicker.value = Date.now(); // 打开选择器显示当前最新时间
  paymentDateRef.value.open()
}
let paymentDatePicker = ref(JSON.stringify(dayjs()))
const paymentDateConfirm = (obj) => {
  OperatingPaymentsList.value.paymentDate = dayjs(obj.value).format('YYYY-MM-DD')
}

//实际付款日期选择
let actualDatePicker = ref(JSON.stringify(dayjs()))
const actualDateRef = ref(null)
const actualDateFn = () => {
  // if (OperatingPaymentsList.value.paymentDate) {
  //   actualDateRef.value.open()
  // } else {
  //   uni.showToast({
  //     title: '请先选择付款日期',
  //     icon: 'none',
  //   })
  // }
  actualDatePicker.value = Date.now(); // 打开选择器显示当前最新时间
  actualDateRef.value.open()
}
const actualDateConfirm = (obj) => {
  OperatingPaymentsList.value.actualDate = dayjs(obj.value).format('YYYY-MM-DD')
}

const associatedTicketsFn = () => {
  uni.navigateTo({
    url: '/components/AssociatedTickets',
  })
}
let attchment = ref([])
const userId = ref(null)
const getDesignDrawingsEmit = (emit) => {
  attchment.value = emit
}
const clickSave = async () => {
  OperatingPaymentsList.value.attchment = attchment.value
  const RequiredItemList = JSON.parse(JSON.stringify(OperatingPaymentsList.value))
  delete RequiredItemList.actualDate
  delete RequiredItemList.remark
  console.log(RequiredItemList, 'RequiredItemList')
  if (isObjectValuesNotEmpty(RequiredItemList)) {
    let params = {
      reason: OperatingPaymentsList.value.reasonPayment,
      costCategory: OperatingPaymentsList.value.costType,
      money: OperatingPaymentsList.value.paymentMoney,
      paymentMethod: OperatingPaymentsList.value.paymentMethods,
      date: dayjs(OperatingPaymentsList.value.paymentDate).format('YYYY-MM-DD hh:mm:ss'),
      payeeFullName: OperatingPaymentsList.value.payee,
      bankAccount: OperatingPaymentsList.value.bankAccount,
      bankOfDeposit: OperatingPaymentsList.value.bankName,
      date2: OperatingPaymentsList.value.actualDate
        ? dayjs(OperatingPaymentsList.value.actualDate).format('YYYY-MM-DD hh:mm:ss')
        : '',
      remark: OperatingPaymentsList.value.remark,
      applyID: listAssociateFile.value.map((v) => v.id), //申请单Id
      ruleId: 'wenlvzhuanxiang',
      type: '文旅运营专项付款申请',
      attchment: JSON.stringify(attchment.value),
      userId: userId.value,
      deptId: OperatingPaymentsList.value.deptId, // 费用承担部门id
      approve: approvalProcessState.state.approvalProcess,
    }
    console.log(params, '提交上传参数params打印')
    let { code } = await addFinancialApplyControllerApi(params)
    if (code === 200) {
      uni.showToast({
        title: '提交成功',
        duration: 1500,
        icon: 'success',
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1000)
    }
  } else {
    uni.showToast({
      title: '请输入完整数据',
      icon: 'error',
    })
  }
}

const list1 = ref([{}])

function debounce(func, wait) {
  let timeout
  return function (...args) {
    const context = this
    clearTimeout(timeout)
    timeout = setTimeout(() => func.apply(context, args), wait)
  }
}

const getUserProfileFn = async () => {
  let { code, data } = await getUserProfile()
  if (code === 200) {
    userId.value = data.userId
    OperatingPaymentsList.value.deptId = data.deptId
  }
}
//获取部门数据
const selectList = ref([])
const geTdepartment = async () => {
  let { code, data } = await listTreeApi()
  if (code === 200) {
      selectList.value = data.slice(57,70)
  }
}
const goHistoryView = () => {
  uni.navigateTo({
    url: '/components/TypeDefineModel?type=文旅运营专项付款申请&apiName=财务',
  })
}
onLoad(async () => {
  await getUserProfileFn()
  geTdepartment()
})
const listAssociateFile = ref([])
const callBackAssociateData = () => {
  listAssociateFile.value = approvalProcessState.state.associateData
}
onShow(() => {
  callBackAssociateData()
})
const deleteFile = (id) => {
  approvalProcessState.mutations.deleteIdAssociateData(approvalProcessState.state, id)
  callBackAssociateData()
}
</script>

<template>
  <view>
    <!--  文旅运营专项付款申请  -->
    <HeaderNavTabarSvg :title="title">
      <image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
    </HeaderNavTabarSvg>
	
	<view class="picker-content" @tap="open = true">
	  <view class="measurement-info-list">
	    <text class="info-list-left isrequired">费用承担部门</text>
	    <view class="info-list-right">
	      <view v-if="OperatingPaymentsList.assumptionCostsDep" class="uni-input"
	      >{{ OperatingPaymentsList.assumptionCostsDep }}
	      </view>
	      <view v-else class="uni-inputs">请选择</view>
	      <i class="iconfont icon-youjiantou"></i>
	    </view>
	  </view>
	</view>
	<liy-select :list="selectList"
	            :open="open"
	            @close="open = false"
	            @change="changeSelect"
	            titleKey="label"
	            subtitleKey="address"
	></liy-select>

    <view class="textarea-box">
      <text class="info-list-left isrequired">付款事由</text>
      <textarea
        v-model="OperatingPaymentsList.reasonPayment"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <view class="picker-content" @tap="costTypeFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">费用种类</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="OperatingPaymentsList.costType"
            >{{ OperatingPaymentsList.costType }}
          </view>
          <view class="uni-inputs" v-else>费用种类</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <uv-picker ref="costTypeRef" :columns="costTypeColumns" @confirm="costTypeConfirm"></uv-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款金额</text>
        <view class="info-list-right">
          <view class="info-content" style="display: flex; align-items: center">
            <input
              adjust-position
              type="text"
              class="uni-input"
              placeholder="请输入"
              v-model="OperatingPaymentsList.paymentMoney"
            />
            <p style="font-size: 28rpx; line-height: 1.4em">元</p>
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="paymentMethodsFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款方式</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="OperatingPaymentsList.paymentMethods"
            >{{ OperatingPaymentsList.paymentMethods }}
          </view>
          <view class="uni-inputs" v-else>请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>

    <uv-picker
      ref="paymentMethodsRef"
      :columns="paymentMethodsColumns"
      @confirm="paymentMethodsConfirm"
    ></uv-picker>

    <view class="picker-content" @tap="paymentDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">付款日期</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="OperatingPaymentsList.paymentDate"
            >{{ OperatingPaymentsList.paymentDate }}
          </view>
          <view class="uni-inputs" v-else>请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="paymentDateRef"
      v-model="paymentDatePicker"
      :formatter="formatter"
      mode="date"
      @confirm="paymentDateConfirm"
    >
    </uv-datetime-picker>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">收款人全称</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              adjust-position
              type="text"
              class="uni-input"
              placeholder="请输入"
              v-model="OperatingPaymentsList.payee"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">银行账号</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              adjust-position
              type="text"
              class="uni-input"
              placeholder="请输入"
              v-model="OperatingPaymentsList.bankAccount"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content">
      <view class="measurement-info-list">
        <text class="info-list-left isrequired">开户行</text>
        <view class="info-list-right">
          <view class="info-content">
            <input
              adjust-position
              type="text"
              class="uni-input"
              placeholder="请输入"
              v-model="OperatingPaymentsList.bankName"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="picker-content" @tap="actualDateFn">
      <view class="measurement-info-list">
        <text class="info-list-left">实际支付日期</text>
        <view class="info-list-right">
          <view class="uni-input" v-if="OperatingPaymentsList.actualDate"
            >{{ OperatingPaymentsList.actualDate }}
          </view>
          <view class="uni-inputs" v-else>请选择</view>
          <i class="iconfont icon-youjiantou"></i>
        </view>
      </view>
    </view>
    <uv-datetime-picker
      ref="actualDateRef"
      v-model="actualDatePicker"
      :formatter="formatter"
      mode="date"
      @confirm="actualDateConfirm"
    >
    </uv-datetime-picker>

    <view class="textarea-box">
      <text class="info-list-left">备注说明</text>
      <textarea
        v-model="OperatingPaymentsList.remark"
        class="textinfo-list"
        placeholder="请输入"
      ></textarea>
    </view>

    <view class="picker-content" @tap="associatedTicketsFn">
      <view class="measurement-info-list">
        <text class="info-list-left">关联申请单</text>
        <view class="info-list-right">
          <image
            slot="icon"
            src="@/static/icon/plus-circle.svg"
            style="width: 32rpx; height: 32rpx"
          ></image>
        </view>
      </view>
    </view>
    <view class="associateClass">
      <view class="list-associate-file" v-for="item in listAssociateFile" :key="item.id">
        <view class="list-associate-file-item-left">
          <image src="/static/icon/file.svg"></image>
          <view class="content-item">
            <view class="item-left">
              <h4>{{ item.nickName }}的{{ item.type }}</h4>
              <text :class="item.applyStatus">{{ item.applyStatus }}</text>
            </view>
            <p>
              提交时间：
              <text>{{ item.createTime }}</text>
            </p>
          </view>
        </view>
        <view class="list-associate-file-item-right">
          <image src="@/static/icon/auditDetailsSVG/close.svg" @click="deleteFile(item.id)"></image>
        </view>
      </view>
    </view>
    <!--    说明附件-->
    <DescriptionAttachment @getDesignDrawingsEmit="getDesignDrawingsEmit" :isrequired="true" />
    <!--    审批流程-->
    <AuditApplicationSteps
      :departmentType="assumptionCostsDepNumParams"
      :ruleId="'wenlvzhuanxiang'"
    />
    <!--    底部占位符-->
    <view class="blank"></view>
    <!--    保存-->
    <view class="buttonContainer">
      <button class="buttonClass" @click="clickSave">提交</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import '@/static/style/index.scss';

.associateClass {
  padding: 20rpx;
  background-color: #fff;

  .list-associate-file {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx;
    border-radius: 8rpx;
    background: #f2f3f5;
    margin-top: 20rpx;

    .list-associate-file-item-left {
      display: flex;
      align-items: center;

      image {
        width: 48rpx;
        height: 48rpx;
      }

      .content-item {
        margin-left: 20rpx;

        .item-left {
          display: flex;
          align-items: center;
        }

        h4 {
          align-self: stretch;
          color: #1d2129;
          font-size: 24rpx;
          font-style: normal;
          font-weight: 400;
          margin-right: 10rpx;
        }

        p {
          align-self: stretch;
          color: #86909c;
          font-size: 22rpx;
          font-style: normal;
          font-weight: 400;
        }
      }
    }

    .list-associate-file-item-right {
      border-radius: 32rpx;
      background: #c4c6c5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
}

.card-list {
  padding: 24rpx 32rpx;
  width: 686rpx;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  background-color: #ffffff;
  border-radius: 16rpx;
  border: 1rpx solid #e5e6eb;
  margin-bottom: 16rpx;

  .header-content,
  .main-content,
  .main-floor {
    @include flex($justify: space-between);
    color: #86909c;
    font-size: 24rpx;
    font-weight: 400;
  }

  .main-content {
    margin: 10rpx 0;

    h4 {
      color: #1d2129;
      font-size: 32rpx;
    }

    p {
      @include flex($justify: center, $align: center);
      padding: 0 8rpx;
      border-radius: 10rpx;
      min-width: 97rpx;
    }

    .待设计,
    .未下单,
    .待核算,
    .待上传安装图片,
    .待上传拆除照片 {
      color: #165dff;
      background-color: #e8f3ff;
    }

    .已核算,
    .已下单,
    .审核通过,
    .已通过 {
      color: #00b42a;
      background-color: #e8ffea;
    }

    .待审核 {
      color: #ff7d00;
      background-color: #fff7e8;
    }

    .驳回 {
      color: $btn-error-color;
      background-color: $btn-error-bg;
    }
  }

  .main-floor {
    .floot-svg {
      @include flex($align: center);

      image {
        width: 28rpx;
        height: 28rpx;
      }

      text {
        color: #1d2129;
      }
    }
  }

  .btn-install-order-list {
    margin-top: 20rpx;

    button {
      height: 56rpx;
      @include flex($justify: center, $align: center);
      border: none;
    }

    .btn-flex {
      @include flex($justify: space-between, $align: center);

      .接单 {
        background-color: #165dff;
        color: #fff;
      }

      button {
        width: 291rpx;
      }
    }

    .进行中 {
      background-color: #e8f3ff;
      color: #165dff;
    }

    .已拒接,
    .拒接 {
      background-color: #ffece8;
      color: #f53f3f;
    }

    .已完工 {
      background-color: #e8ffea;
      color: #00b42a;
    }

    .已超时 {
      background-color: #fff7e8;
      color: #ff7d00;
    }

    .row-btn {
      width: 100%;
    }
  }
}

.bukaSvg {
  width: 32rpx;
  height: 32rpx;
}

.buttonContainer {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
  align-items: center;
  padding: 16rpx;
  background-color: #fff;
}

.buttonClass {
  display: flex;
  width: 686rpx;
  height: 72rpx;
  justify-content: center;
  align-items: center;
  background: #165dff;
  border-radius: 16rpx;
  color: #fff;
  font-size: 32rpx;
  //margin-top: 40rpx;
  overflow: hidden;
}

.uni-inputs {
  color: #86909c !important;
}

.uni-input {
  text-align: right;
  font-size: 28rpx;
}

.blank {
  height: calc(72rpx + 32rpx + 20rpx);
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.bukabanci {
  .info-content {
    color: #86909c !important;
  }

  .info-list-left {
    // @include font-color-auxiliary;
    color: #86909c !important;
  }
}

.textarea-box {
  background: #fff;
  padding: 20rpx;
  margin-top: 20rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .textinfo-list {
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
  }
}

.popup-content {
  background: #f7f8fa;

  .header-box {
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    p {
      color: #1d2129;
      font-size: 34rpx;
      font-weight: 500;
    }

    .module-image-ba {
      border-radius: 32rpx;
      background: #f2f3f5;
      width: 45rpx;
      height: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      image {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
}

.measurement-info-list {
  @include flex($align: center, $justify: space-between);
  padding: $spacing-row-base;
  background: #fff;
  margin-top: 16rpx;

  .info-list-left {
    // @include font-color-auxiliary;
    color: #1d2129;
    font-size: $font-size-base;
  }

  .info-list-right {
    @include flex($align: center);

    .info-content {
      @include textStyleBase;
      font-size: 34rpx;
    }

    .iconfont {
      margin-left: 16rpx;
      // @include font-color-auxiliary;
      color: $uni-fc-fu-lin;
    }
  }

  .unit {
    // @include font-color-auxiliary;
    color: $uni-fc-fu-lin;
  }
}
</style>
